package com.alan.db.android;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.alan.db.LogUtil;
import com.alan.db.android.patcher.AndroidPatcher;
import com.alan.db.android.patcher.IAndroidPatcher;
import com.alan.db.base.DbModel;
import com.alan.db.table.Table;
import com.alan.db.table.TableFactory;


import java.io.File;
import java.util.List;

/**
 * sqlite数据库管理类，负责表的初始化和表的更
 *
 * @author Alan
 */
public class AndroidSQLiteManager extends SQLiteOpenHelper {
    protected static volatile AndroidSQLiteManager instance;
    protected Context mContext;

    protected static IAndroidDbConfig mConfig;
    protected static volatile SQLiteDatabase db;

    protected AndroidSQLiteManager(Context context, String name, int version) {
        super(context, name, null, version);
        mContext = context;
    }

    public static synchronized SQLiteDatabase getDB(Context context,
                                                    IAndroidDbConfig config) {
        if (db == null) {
            mConfig = config;
            if (instance == null) {
                instance = new AndroidSQLiteManager(context, config.getDatabaseName(),
                        config.getDatabaseVersion());
            }
            db = instance.getWritableDatabase();

            config.attatch(db);
        }
        return db;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        LogUtil.d("表：" + "开始创建");
        List<Class<? extends DbModel>> classes = mConfig.getTables(mContext);
        for (Class<? extends DbModel> clazz : classes) {
            try {
                Table table = TableFactory.getTable(clazz);
                String sql = table.getCreateSqlStr();
                LogUtil.d(sql);
                db.execSQL(sql);
                LogUtil.d("表" + table.getTableName());
            } catch (Exception e) {
                LogUtil.error(e);
            }
        }
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        List<Class<? extends DbModel>> classes = mConfig.getTables(mContext);
        LogUtil.d("onUpgrade");
        for (Class<? extends DbModel> clazz : classes) {
            try {
                AndroidPatcher annotation = clazz.getAnnotation(AndroidPatcher.class);
                Class<? extends IAndroidPatcher>[] name = annotation.name();
                if (null != name && name.length > 0) {
                    for (Class<? extends IAndroidPatcher> patcher : name) {
                        try {
                            LogUtil.d("onUpgrade step find");
                            IAndroidPatcher inst = patcher.newInstance();
                            int max = inst.getSupportMaxVersion();
                            if (oldVersion < max) {
                                LogUtil.d("onUpgrade step execute");
                                inst.execute(db);
                            }
                        } catch (Exception e) {
                            LogUtil.error(e);
                        }
                    }
                }
            } catch (Exception e) {
                LogUtil.error(e);
            }
        }
    }


    public static void destroy() {
        if (null != db) {
            db.close();
            db = null;
            instance = null;
        }
    }

    public static void attach(File file, SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("attach DATABASE '" + file.getAbsolutePath() + "' AS 'c'");
    }
}
